home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8957 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: how can an int data t
  5. Date: 7 Mar 1996 07:56:17 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4hm4r1$gtp@gail.ripco.com>
  8. NNTP-Posting-Host: cook.ripco.com
  9.  
  10. Howard Salmon <captarm@azstarnet.com>
  11. in <4hg2si$irt@news.azstarnet.com> asks:
  12.  
  13. >I thought that the int data type could only accept integer data;
  14. >however, the example listed below (taken from Dave Mark's book "Learn C
  15. >on the Macintosh") shows an int variable ("done") accepting character
  16. >data (i.e. "FALSE")....
  17.  
  18. [snip]
  19.  
  20. >#include <stdio.h>
  21.  
  22. >main()
  23. >{
  24. >        int             startingPoint, candidate, i;
  25. >        int             done, foundFactor;
  26.  
  27. >        done = FALSE;
  28.  
  29. There are two different misperceptions that you may have.  The choice
  30. between them depends on what you mean by `character data'.  I suspect
  31. that you have in mind the first below; if you were a little more
  32. experienced as a C programmer, I would suspect the second.
  33.  
  34. 1) You may think FALSE is a string.  "FALSE" would be a string, FALSE is
  35. an identifier.  The problem with FALSE is that it has no value.  I guess
  36. the Macintosh compiler Dave Mark that assumes improperly gives FALSE a
  37. value in stdio.h.
  38.  
  39. This is probably done via macros:
  40.     #define FALSE 0
  41.     #define TRUE 1
  42. or enums:
  43.     typedef enum {FALSE, TRUE} BOOLEAN;
  44. In either case, not only should these not be in a system header and are
  45. (in this code) otherwise undefined, but also can lead to logical errors
  46. if not used with care.
  47.  
  48. 2) You may think that FALSE is actually "character data", e.g.
  49.     const char FALSE = 0;
  50. rather than a string.  An assignment of a char to an int is quite legal:
  51. char is an integral type, and int is another.  The char value is
  52. promoted to an int and assigned without any problems.  Whether char is
  53. signed or unsigned raises different questions when the MSB=1.
  54.                                  
  55. --
  56. * Martin Ambuhl       net: mambuhl@ripco.com
  57. * Chicago, IL (USA)    
  58.